package com.fingard.dsp.bank.directbank.cgb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.math.BigDecimal;


public class CGB02QryRefund extends CGB02Base implements IQryRefund {
    @Override
    public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
        RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
        retAts.writeStart();
        int tmpSuccCount = 0;
        BigDecimal tmpTotalAmt = new BigDecimal("0");
        String pageNo = "";
        CGB02Header bankHeader;
        XmlTextWriter xmlWriter;

        String[] tmpRetBkStr;

        try {
            do{
                bankHeader = new CGB02Header();
                bankHeader.reqSeqID = getQryTransID();
                bankHeader.transCode = "1024";

                xmlWriter = new XmlTextWriter();
                xmlWriter.autoFormat = false;
                writeCommonHeader(xmlWriter, bankHeader);

                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("account", p_req.payAct);
                xmlWriter.writeElementString("beginDate", Format.Date8Format.format(p_req.getQryStartDate()));
                xmlWriter.writeElementString("endDate", Format.Date8Format.format(p_req.getQryEndDate()));
                xmlWriter.writeElementString("pageRows", "");
                xmlWriter.writeElementString("pageNo", pageNo);
                xmlWriter.writeEndDocument();

                tmpRetBkStr = sendToBank(xmlWriter.toXmlString());

                if(tmpRetBkStr[0].length() != 0){
                    return returnRetAtsQryRefund(p_req.reqHeader, "-1", tmpRetBkStr[1]);
                }

                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if(tmpBkReader == null){
                    return returnRetAtsQryRefund(p_req.reqHeader, "-1", tmpSbMsg.toString());
                }

                String retCode = tmpBkReader.getFirstText("/BEDC/Message/commHead/retCode");
                if(!"000".equals(retCode)){
                    return returnRetAtsQryRefund(p_req.reqHeader, "-1", "查询失败，错误编码：" + retCode);
                }

                pageNo = tmpBkReader.getFirstText("/BEDC/Message/Body/pageNo");
                RetAtsRefundDetail tmpRetDetail;
                XElement tmpRetNode;
                while (tmpBkReader.read("/BEDC/Message/Body/records/record")) {
                    tmpRetDetail = new RetAtsRefundDetail();
                    tmpRetNode = tmpBkReader.readedXElement;
                    tmpRetDetail.askNum = tmpRetNode.getSingleText("serialNo1");
                    tmpRetDetail.payAct = tmpRetNode.getSingleText("inAcc");
                    tmpRetDetail.recAct = tmpRetNode.getSingleText("outAcc");
                    tmpRetDetail.amount = tmpRetNode.getSingleText("amount");
                    String dealDate = tmpRetNode.getSingleText("dealDate");
                    tmpRetDetail.refundTicketDate = Format.Date10Format.format(Format.DateTime14Format.parse(dealDate));
                    tmpRetDetail.submitTime = tmpRetDetail.refundTicketDate + " 00:00:00";
                    tmpRetDetail.refundTicketReason = tmpRetNode.getSingleText("reason");
                    tmpSuccCount++;
                    tmpTotalAmt = tmpTotalAmt.add(new BigDecimal(tmpRetDetail.amount));
                    retAts.writeRetDetailOnly(tmpRetDetail);
                }
            }while (!StringHelper.isNullOrEmpty((pageNo)));

        } catch (Exception ex) {
            ex.printStackTrace();
            WriteBankLogLn(ex);
            return returnRetAtsQryRefund(p_req.reqHeader, "-1", ex.getMessage());
        }
        retAts.totleAmount = String.valueOf(tmpTotalAmt);
        retAts.totleNum = String.valueOf(tmpSuccCount);
        if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("当前时间范围内无退票数据！");
        }
        retAts.writeEnd();
        return retAts;
    }

    private RetAtsQryRefund returnRetAtsQryRefund(ReqRespHeader header, String code, String msg) throws Exception {
        RetAtsQryRefund retAts = new RetAtsQryRefund(header);
        retAts.writeStart();
        retAts.writeRespCode(code);
        retAts.writeRespInfo(msg);
        retAts.writeEnd();
        return retAts;
    }

}
